package activemq_chap7;

import javax.jms.Connection;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class Receiver {
	private static ActiveMQConnectionFactory factory;
	private static Connection connection;
	private static Session session;
	private MessageConsumer consumer;

	public void receiveMessage() {
		try {
			factory = new ActiveMQConnectionFactory(
					ActiveMQConnectionFactory.DEFAULT_BROKER_BIND_URL);
			connection = factory.createConnection("admin", "admin");
			connection.start();
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Queue queue = session.createQueue("tinhcd");
			consumer = session.createConsumer(queue);
			Message message = consumer.receive();
			TextMessage textMessage = (TextMessage) message;
			System.out.println("I have recieved message !");
			System.out.println(textMessage.getText());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Receiver receiver = new Receiver();
		receiver.receiveMessage();
	}
}
